package com.haiyou.dc.thread.test3;

import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;

import java.util.Date;

import static org.quartz.DateBuilder.evenSecondDateAfterNow;
import static org.quartz.JobBuilder.newJob;
import static org.quartz.SimpleScheduleBuilder.simpleSchedule;
import static org.quartz.TriggerBuilder.newTrigger;

/**
 *Quart使用：
 * 1、创建调度工厂
 * 2、创建调度计划
 * 3、创建任务
 * 4、创建触发器
 * 5、开启计划
 * 6、关闭
 */
public class TestQuartz {

    public static void run() throws SchedulerException {
        //1、创建调度工厂
        SchedulerFactory sf = new StdSchedulerFactory();
        //2、创建调度计划
        Scheduler sched = sf.getScheduler();
        //3、创建任务
        JobDetail job = newJob(HelloJob.class).withIdentity("job1", "group1").build();

        //下一秒触发
        Date runTime = evenSecondDateAfterNow();
        //4、创建触发器
//        Trigger trigger = newTrigger().withIdentity("trigger1", "group1").startAt(runTime).build();
        //间隔5秒，重复3次
        Trigger trigger = newTrigger().withIdentity("trigger1", "group1")
                .withSchedule(simpleSchedule().withIntervalInSeconds(5).withRepeatCount(3))
                .startAt(runTime).build();
        sched.scheduleJob(job, trigger);

        //5、开启计划
        sched.start();

        try {

            Thread.sleep(20L * 1000L);
            // executing...
        } catch (Exception e) {
            //
        }

        //6、关闭
        sched.shutdown(true);
    }

    public static void main(String[] args) throws SchedulerException {
        run();
    }
}
